package com.ruoyi.books.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.books.domain.BooksAll;
import com.ruoyi.books.mapper.BooksCategoryMapper;
import com.ruoyi.books.domain.BooksCategory;
import com.ruoyi.books.service.IBooksCategoryService;

/**
 * 非遗分类汇总Service业务层处理
 * 
 * @author FeiYiLand
 * @date 2024-03-25
 */
@Service
public class BooksCategoryServiceImpl implements IBooksCategoryService 
{
    @Autowired
    private BooksCategoryMapper booksCategoryMapper;

    /**
     * 查询非遗分类汇总
     * 
     * @param categoryId 非遗分类汇总主键
     * @return 非遗分类汇总
     */
    @Override
    public BooksCategory selectBooksCategoryByCategoryId(Long categoryId)
    {
        return booksCategoryMapper.selectBooksCategoryByCategoryId(categoryId);
    }

    /**
     * 查询非遗分类汇总列表
     * 
     * @param booksCategory 非遗分类汇总
     * @return 非遗分类汇总
     */
    @Override
    public List<BooksCategory> selectBooksCategoryList(BooksCategory booksCategory)
    {
        return booksCategoryMapper.selectBooksCategoryList(booksCategory);
    }

    /**
     * 新增非遗分类汇总
     * 
     * @param booksCategory 非遗分类汇总
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBooksCategory(BooksCategory booksCategory)
    {
        int rows = booksCategoryMapper.insertBooksCategory(booksCategory);
        insertBooksAll(booksCategory);
        return rows;
    }

    /**
     * 修改非遗分类汇总
     * 
     * @param booksCategory 非遗分类汇总
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBooksCategory(BooksCategory booksCategory)
    {
        booksCategoryMapper.deleteBooksAllByAllId(booksCategory.getCategoryId());
        insertBooksAll(booksCategory);
        return booksCategoryMapper.updateBooksCategory(booksCategory);
    }

    /**
     * 批量删除非遗分类汇总
     * 
     * @param categoryIds 需要删除的非遗分类汇总主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBooksCategoryByCategoryIds(Long[] categoryIds)
    {
        booksCategoryMapper.deleteBooksAllByAllIds(categoryIds);
        return booksCategoryMapper.deleteBooksCategoryByCategoryIds(categoryIds);
    }

    /**
     * 删除非遗分类汇总信息
     * 
     * @param categoryId 非遗分类汇总主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBooksCategoryByCategoryId(Long categoryId)
    {
        booksCategoryMapper.deleteBooksAllByAllId(categoryId);
        return booksCategoryMapper.deleteBooksCategoryByCategoryId(categoryId);
    }

    /**
     * 新增非遗项目总信息
     * 
     * @param booksCategory 非遗分类汇总对象
     */
    public void insertBooksAll(BooksCategory booksCategory)
    {
        List<BooksAll> booksAllList = booksCategory.getBooksAllList();
        Long categoryId = booksCategory.getCategoryId();
        if (StringUtils.isNotNull(booksAllList))
        {
            List<BooksAll> list = new ArrayList<BooksAll>();
            for (BooksAll booksAll : booksAllList)
            {
                booksAll.setAllId(categoryId);
                list.add(booksAll);
            }
            if (list.size() > 0)
            {
                booksCategoryMapper.batchBooksAll(list);
            }
        }
    }
}
